package com.amazon.ags.client.whispersync.zip;

import android.text.TextUtils;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class SimpleZipUtil
  implements ZipUtil
{
  private static final String FEATURE_NAME = "STC";
  private static final int KIBI = 1024;
  private static final String TAG = "STC_" + SimpleZipUtil.class.getSimpleName();
  private FilenameFilter currentFilter;
  private boolean hasFile;
  private ZipInputStream zipInputStream;
  private ZipOutputStream zipOutputStream;

  private void addWithFilter(File paramFile, String paramString)
    throws IOException
  {
    Log.d(TAG, "adding file " + paramFile.getName() + " to [" + paramString + "]");
    String str1 = paramString;
    if (!TextUtils.isEmpty(str1))
      str1 = str1 + "/";
    String str2 = str1 + paramFile.getName();
    if (paramFile.isDirectory())
    {
      File[] arrayOfFile = paramFile.listFiles(this.currentFilter);
      int i = arrayOfFile.length;
      for (int j = 0; j < i; j++)
        addWithFilter(arrayOfFile[j], str2);
    }
    zipFileToStream(paramFile, str2);
    this.hasFile = true;
  }

  private ZipEntry getNextEntry(ZipInputStream paramZipInputStream)
  {
    try
    {
      ZipEntry localZipEntry = paramZipInputStream.getNextEntry();
      return localZipEntry;
    }
    catch (EOFException localEOFException)
    {
      Log.d(TAG, "Ignoring EOFException");
      return null;
    }
    catch (IOException localIOException)
    {
    }
    return null;
  }

  private void unzipFile(File paramFile)
    throws IOException
  {
    paramFile.getParentFile().mkdirs();
    byte[] arrayOfByte = new byte[1024];
    try
    {
      FileOutputStream localFileOutputStream1 = new FileOutputStream(paramFile);
      try
      {
        while (true)
        {
          int i = this.zipInputStream.read(arrayOfByte);
          if (i == -1)
            break;
          localFileOutputStream1.write(arrayOfByte, 0, i);
        }
      }
      finally
      {
        localFileOutputStream2 = localFileOutputStream1;
      }
      this.zipInputStream.closeEntry();
      if (localFileOutputStream2 != null)
        localFileOutputStream2.close();
      throw localObject1;
      this.zipInputStream.closeEntry();
      if (localFileOutputStream1 != null)
        localFileOutputStream1.close();
      return;
    }
    finally
    {
      while (true)
        FileOutputStream localFileOutputStream2 = null;
    }
  }

  private void zipFileToStream(File paramFile, String paramString)
    throws IOException
  {
    try
    {
      ZipEntry localZipEntry = new ZipEntry(paramString);
      localZipEntry.setTime(0L);
      this.zipOutputStream.putNextEntry(localZipEntry);
      FileInputStream localFileInputStream2 = new FileInputStream(paramFile);
      try
      {
        byte[] arrayOfByte = new byte[1024];
        while (true)
        {
          int i = localFileInputStream2.read(arrayOfByte);
          if (i <= 0)
            break;
          this.zipOutputStream.write(arrayOfByte, 0, i);
        }
      }
      finally
      {
        localFileInputStream1 = localFileInputStream2;
      }
      if (localFileInputStream1 != null);
      try
      {
        localFileInputStream1.close();
        if (this.zipOutputStream != null)
          this.zipOutputStream.closeEntry();
        throw localObject1;
        if (localFileInputStream2 != null);
        try
        {
          localFileInputStream2.close();
          return;
        }
        finally
        {
          if (this.zipOutputStream != null)
            this.zipOutputStream.closeEntry();
        }
      }
      finally
      {
        if (this.zipOutputStream != null)
          this.zipOutputStream.closeEntry();
      }
    }
    finally
    {
      FileInputStream localFileInputStream1 = null;
    }
  }

  public final void unzip(InputStream paramInputStream, File paramFile)
    throws IOException
  {
    if (paramInputStream == null)
      throw new IllegalArgumentException("source cannot be null");
    if (paramFile == null)
      throw new IllegalArgumentException("destinationDir cannot be null");
    Log.d(TAG, "Entering unzip() with destination directory [" + paramFile.getPath() + "]");
    this.zipInputStream = new ZipInputStream(paramInputStream);
    while (true)
    {
      File localFile;
      try
      {
        ZipEntry localZipEntry = getNextEntry(this.zipInputStream);
        if (localZipEntry == null)
          break;
        localFile = new File(paramFile, localZipEntry.getName());
        if (localZipEntry.isDirectory())
        {
          Log.d(TAG, "Creating dir: " + localFile.getName());
          localFile.mkdirs();
          continue;
        }
      }
      finally
      {
        if (this.zipInputStream != null)
          this.zipInputStream.close();
      }
      Log.d(TAG, "Creating file: " + localFile.getName());
      unzipFile(localFile);
    }
    if (this.zipInputStream != null)
      this.zipInputStream.close();
  }

  public final void unzip(byte[] paramArrayOfByte, File paramFile)
    throws IOException
  {
    if (paramArrayOfByte == null)
      throw new IllegalArgumentException("source cannot be null");
    unzip(new ByteArrayInputStream(paramArrayOfByte), paramFile);
  }

  public final byte[] zipToByteArray(File paramFile, FilenameFilter paramFilenameFilter)
    throws IOException
  {
    if (paramFile == null)
      throw new IllegalArgumentException("baseDir cannot be null");
    if (paramFilenameFilter == null)
      throw new IllegalArgumentException("filter cannot be null");
    this.hasFile = false;
    this.currentFilter = paramFilenameFilter;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    this.zipOutputStream = new ZipOutputStream(localByteArrayOutputStream);
    File[] arrayOfFile = paramFile.listFiles(paramFilenameFilter);
    int i = arrayOfFile.length;
    for (int j = 0; j < i; j++)
      addWithFilter(arrayOfFile[j], "");
    byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
    try
    {
      this.zipOutputStream.close();
      if (!this.hasFile)
      {
        Log.d(TAG, "No files, cannot create zip file");
        arrayOfByte = null;
      }
      return arrayOfByte;
    }
    catch (ZipException localZipException)
    {
      Log.d(TAG, "No files found.  Returning null", localZipException);
    }
    return null;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.whispersync.zip.SimpleZipUtil
 * JD-Core Version:    0.6.2
 */